package com.juli.school.web.controller;

import java.io.FileInputStream;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.juli.school.service.impl.PoiService;

/**
 * @author wjp
 * @date 2017年7月31日
 */
@Controller
@RequestMapping("poi")
public class PoiController {
	@Autowired
	private PoiService poiService;

	/**
	 * 跳到上传文件页面
	 * 
	 * @return
	 */
	@RequestMapping("importFileUI")
	public String importExcelUI() {
		return "import/importFile";
	}

	/**
	 * 导入文件(支持大批量数据导入)
	 * 
	 * @return
	 */
	@RequestMapping(value = "importFile", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	// produces = "application/json; charset=utf-8" 解决直接返回String时转换json中文乱码问题
	@ResponseBody
	public String importFile(@RequestParam("myRegion") CommonsMultipartFile myRegion) {
		FileInputStream fis;
		try {
			fis = (FileInputStream) myRegion.getInputStream(); // 获取上传文件流
			String fileName = myRegion.getOriginalFilename();// 获取上传文件名
			int time = poiService.importExcel(fis, fileName);// 进行导入文件并返回一个导入所需时间，若为0则导入失败
			if (time == 0) {
				return new StringBuilder().append("导入失败").toString();
			}
			return new StringBuilder().append("导入成功，共花费：").append(time).append("秒").toString();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		return new StringBuilder().append("导入失败").toString();
	}
}
